package com.china08.yxyapi.api.home.activity.lectureresult;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.huodong.lecture.YxyHuodongLectureResultArea;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.lectureresult.YxyHuodongLectureResultAreaRepository;

/**
 * 哈尔滨市按区域统计
 *
 */
@CrossOrigin
@RestController
public class LectureResultAreaController extends YxyApiBaseController {

    @Autowired
    private YxyHuodongLectureResultAreaRepository lectureResultAreaRepository;

    @RequestMapping(value = "/api_yw/home/activity/lectureresult/area", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody(required = false) ReqModel reqModel) {

        Pageable pageable = reqModel.getPageable(Direction.DESC, "packCount");
        Page<YxyHuodongLectureResultArea> findAll = lectureResultAreaRepository.findAll(pageable);

        RespModel respModel = null;
        List<RespModel> list = new ArrayList<RespModel>();
        Integer rank = 1;
        for (YxyHuodongLectureResultArea yxyHuodongLectureResultArea : findAll) {
            respModel = new RespModel();
            respModel.rank = pageable.getPageSize() * pageable.getPageNumber() + rank;
            rank++;
            respModel.areaName = yxyHuodongLectureResultArea.getAreaNameDistrict();
            respModel.teaCount = yxyHuodongLectureResultArea.getTeaCount();
            respModel.packCount = yxyHuodongLectureResultArea.getPackCount();
            list.add(respModel);
        }
        Page<RespModel> page = new PageImpl<>(list, pageable, findAll.getTotalElements());
        return page;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {

        public Integer rank;

        public String areaName;

        public Integer teaCount;

        public Long packCount;
    }
}
